﻿@model IEnumerable<Superstore.Store.Models.ShoppingCartGoodsInfo>
<div class="activeGoodsDoc">
    @foreach (var item in Model)
    {
        <div class="IndexPart">
            <div class="IndexGoodsImg">
                <img class="GoodsImg" src="@item.goods_info.image">
            </div>
            <div class="IndexGoodsTxt">
                <h1>@item.goods_info.name </h1>
            </div>
            <section>
                <span>￥@item.goods_info.price </span>
                <div class="IndexGoodsNum">
                    <a class="dec_count_@item.goods_info.goodsid" onclick="adjustPromotionNum(@item.goods_info.goodsid, @item.main_tab, @item.sub_tab, -1)"><img src="/images/Dec.png"></a>
                    <span id="goodsid_@item.goods_info.goodsid">@item.goods_num</span>
                    <a class="add_count_@item.goods_info.goodsid" onclick="adjustPromotionNum(@item.goods_info.goodsid, @item.main_tab, @item.sub_tab, 1)"><img src="/images/Add.png"></a>
                </div>
            </section>
        </div>
    }
</div>

<!--添加购物车JS-->
<script>
    $(function () {
        //当前浏览器窗口高度 考虑到header、购物车和活动图的高度 45 + 50 + 116 = 211
        var currentWindowHeight = $(window).height() - 211;
        var attrHeight = "height:" + currentWindowHeight + "px";
        //当前容器确定高度
        $("#goodsList").attr("style", attrHeight);
        
        scrollBegin = 0;
        setGoodsListScrollListener();
        adjustGoodsList();
    });

    
    function setGoodsListScrollListener() {
        var goodsWindow = $("#goodsList");
        var goodsDoc = $(".activeGoodsDoc");
        isScroll(goodsWindow, goodsDoc, function () {
            scrollBegin = GoodsMgr.getCurTagGoodsNum();
            if (scrollBegin > 0) {//滚动都是来自已有列表，如果是空列表，就不触发这个事件，由click载入
                GoodsMgr.getGoodsList(scrollBegin, GoodsMgr.goodsQueryNum, addGoodsList);
            }
        });
    }
    function isScroll(scrollWindow, scrollDoc, scrollCallBack) {
        $(scrollWindow).scroll(function () {
            var scrollTop = $(this).scrollTop();
            var windowHeight = $(this).height();
            var docHeight = $(scrollDoc).height();
            if (scrollTop >= (docHeight - windowHeight)) {
                if (scrollCallBack) {
                    scrollCallBack();
                };
            }
        });
    }
    function adjustGoodsList() {
        var goodsList = PromotionMgr.getPromotionGoodsList();
        $('.IndexPart .IndexGoodsNum').each(function (index) {
            var goodsNum = Number(goodsList[index].goods_num);
            if (goodsNum <= 0) {
                $('.IndexPart .IndexGoodsNum a').eq(2 * index).hide();
                $('.IndexPart .IndexGoodsNum span').eq(index).hide();
            } else {
                $('.IndexPart .IndexGoodsNum a').eq(2 * index).show();
                $('.IndexPart .IndexGoodsNum span').eq(index).show();
            }
        });
    }

    function adjustGoodsListShow(goodsid, goodsNum) {
        if (goodsNum > 0) {
            $(".dec_count_" + goodsid).show();
            $("#goodsid_" + goodsid).show();
            $("#goodsid_" + goodsid).text(goodsNum);
        } else {
            $(".dec_count_" + goodsid).hide();
            $("#goodsid_" + goodsid).hide();
        }
    }
    function adjustGoodsListShow(goodsid, goodsNum) {
        if (goodsNum > 0) {
            $(".dec_count_" + goodsid).show();
            $("#goodsid_" + goodsid).show();
            $("#goodsid_" + goodsid).text(goodsNum);
        } else {
            $(".dec_count_" + goodsid).hide();
            $("#goodsid_" + goodsid).hide();
        }
    }
    function adjustPromotionGoodsNumShow(goodsid, goodsNum, shoppingList) {
        adjustGoodsListShow(goodsid, goodsNum);

        var url = '@Url.Content("~/")' + 'Home/Shopping';
        NativeAjax(shoppingList, url, function (msg) {
            loadPartialViewResult(msg, $("#shopping"), 1);
        });
    }

    function adjustPromotionNum(goodsId, mainTab, subTab, typeOp) {
        ShoppingMgr.adjustGoodsNum(goodsId, mainTab, subTab, typeOp, adjustPromotionGoodsNumShow);
    }
</script>